﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

using Xc.DataAccess.Core;
using Xc.DataAccess.DataBase.DeviceInfos.Models;
using System.Data;
using System.Xml.Linq;
using YamlDotNet.Core.Tokens;
using Xc.DataAccess.DataBase.DeviceInfos.Extensions;

namespace Xc.DataAccess.DataBase.DeviceInfos
{
    /// <summary>
    /// 设备属性信息服务
    /// </summary>
    /// <typeparam name="TDbContext"></typeparam>
    public class DbDeviceInfoPropertyService<TDbContext> : TableInfoService<TDbContext, DevicePropertyInfo, DevicePropertyInfoHistory>, IDbDeviceInfoPropertyService where TDbContext : DbContext, IDbDevice
    {
        /// <summary>
        /// 设备报警服务
        /// </summary>
        /// <param name="dbContextFactory"></param>
        public DbDeviceInfoPropertyService(IDbContextFactory<TDbContext> dbContextFactory) : base(dbContextFactory)
        {

        }
        /// <summary>
        /// 删除指定属性信息
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public override async Task<Result> Remove(string deviceId, string name)
        {
            using var db = _dbContextFactory.CreateDbContext();
            var alarms = await db.DevicePropertyInfos.Where(p => p.DeviceInfoId == deviceId && p.Name == name).ToArrayAsync();
            db.DevicePropertyInfos.RemoveRange(alarms);
            return Result.From(db.SaveChanges(), s => s > 0);
        }
        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public override Task<Result> Set(Dictionary<string, UnitString> valuePairs, string deviceId)
        {
          return Task.Run(() =>
          {
              var dt = DateTime.Now;
              using var db = _dbContextFactory.CreateDbContext();
              return this.SetValue(db, db.DevicePropertyInfos, db.DevicePropertyInfoHistorys, valuePairs, deviceId);
          });
        }

        /// <summary>
        /// 获取属性
        /// </summary>
        /// <param name="deviceid"></param>
        /// <returns></returns>
        public override ResultData<DevicePropertyInfo[]> Gets(string deviceid)
        {
            using var db = _dbContextFactory.CreateDbContext();
            var properties = db.DevicePropertyInfos.Where(p => p.DeviceInfoId == deviceid);
            return ResultData<DevicePropertyInfo[]>.Accept("获取成功", properties.ToArray());
        }
        /// <summary>
        /// 获取历史属性信息
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="dateTimeStart"></param>
        /// <param name="dateTimeEnd"></param>
        /// <returns></returns>
        public override Task<DevicePropertyInfoHistory[]> Gets(string deviceId, DateTime? dateTimeStart, DateTime? dateTimeEnd)
        {
            using var db = _dbContextFactory.CreateDbContext();
            var properties = db.DevicePropertyInfoHistorys.Where(p => p.DeviceInfoId == deviceId).Between(dateTimeStart, dateTimeEnd);
            return properties.ToArrayAsync();
        }
    }

}
